package com.servlet.user;

import com.entity.Code;
import com.service.UserService;
import com.utils.JsonUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 使修改密码时，使用异步实时查询密码是否正确，如果不正确。及时提醒用户该密码出错，提高用户体验感3
 * 必选参数：id(用户ID)、password
 */
@WebServlet(name = "FindPdById", value = "/FindPdById")
public class FindPdById extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String password = request.getParameter("password");
        UserService userService = new UserService();
        JsonUtil jsonUtil = new JsonUtil();
        try {
            String user_password = userService.findPdById(id);
            if (user_password.equals(password)){
                //如果密码相等，则返回200
                Code code = new Code();
                code.setMsg("密码正确");
                code.setCode("200");
                String s = jsonUtil.JavaToJson(code);
                response.getWriter().print(s);
            }else {
                //密码错误 返回402
                Code code = new Code();
                code.setMsg("密码错误");
                code.setCode("403");
                String s = jsonUtil.JavaToJson(code);
                response.getWriter().print(s);

            }
        }catch (Exception e){
            Code code = new Code();
            code.setMsg("该用户ID不存在，请稍后再试");
            code.setCode("400");
            String s = jsonUtil.JavaToJson(code);
            response.getWriter().print(s);
        }
    }
}
